package cc.rengu.redp.bizimpl.upmp.controller;

import cc.rengu.redp.bizimpl.upmp.domain.BBaseProdInfo;
import cc.rengu.redp.bizimpl.upmp.service.IBBaseProdInfoService;
import cc.rengu.redp.common.annotation.Log;
import cc.rengu.redp.common.controller.BaseController;
import cc.rengu.redp.common.domain.QueryRequest;
import cc.rengu.redp.common.exception.RedpException;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.validation.constraints.NotBlank;
import java.util.Map;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author 刘玉亮.LL
 * @since 2021-06-10
 */
@Slf4j
@Api(tags = "产品管理:基础产品管理")
@RequestMapping("/admin/mcmp/bBaseProdInfo")
@RestController
public class BBaseProdInfoController extends BaseController {

    @Autowired
    private IBBaseProdInfoService bBaseProdInfoService;

    @GetMapping
//    @RequiresPermissions("areaInfo:view")
    public Map<String, Object> list(QueryRequest request, BBaseProdInfo entity) {
        return getDataTable(this.bBaseProdInfoService.findByPage(request, entity));
    }

    @Log("查询单条")
    @GetMapping(value = "/getOne")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "instId", value = "机构标识", required = true, dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "baseProdCode", value = "产品编码", required = true, dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "id", value = "主键", dataType = "String")
    })
    public BBaseProdInfo getOne(@NotBlank(message = "{required}") String
                                            baseProdCode,  String
                                            id, @NotBlank(message = "{required}") String instId) {
        if(StringUtils.isNotBlank(id)){
         return bBaseProdInfoService.getById(id);
        }
        LambdaQueryWrapper<BBaseProdInfo> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(BBaseProdInfo::getInstId, instId);
        wrapper.eq(BBaseProdInfo::getBaseProdCode, baseProdCode);
        return bBaseProdInfoService.getOne(wrapper);
    }

    @Log("编辑查询")
    @GetMapping(value = "/edit")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "instId", value = "机构标识", required = true, dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "baseProdCode", value = "产品编码", required = true, dataType = "String")
    })
    public Map<String,Object>  getEdit(@NotBlank(message = "{required}") String
                                             baseProdCode, @NotBlank(message = "{required}") String instId) throws RedpException {
        try {
            return bBaseProdInfoService.getEdit(baseProdCode,instId);
        } catch (RedpException e) {
            String message = "查询失败";
            log.error(message, e);
            throw new RedpException(message);
        }
    }


    @Log("修改")
    @PutMapping("/update")
//    @RequiresPermissions("areaInfo:update")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "optFlag", value = "主操作类型:新增|修改|删除|审核等", required = true, dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "instId", value = "机构标识", required = true, dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "baseProdCode", value = "产品编码", required = true, dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "id", value = "主键", dataType = "String")
          })
    public void update(@Valid BBaseProdInfo entity) throws RedpException {
        try {
            this.bBaseProdInfoService.update(entity);
        } catch (Exception e) {
            String message = "修改失败";
            log.error(message, e);
            throw new RedpException(message);
        }
    }
}
